﻿/*
 创建人：盛金龙
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using AjaxPro;

namespace EnvironmentManagerSystem.HuanJing
{
    public partial class TreePanel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                DBBing();
            }
        }
        public void DBBing()
        {
            DataTable dt = TreeViewPanel.GetTree();
            if (dt != null)
            {
                //tree.Attributes.Add("onclick", "var o = window.event.srcElement; if (o.tagName == 'INPUT' && o.type == 'checkbox') {__doPostBack('', ''); }");
                //tree.Attributes.Add("onclick", "client_OnTreeNodeChecked();");
                tree.Nodes.Clear();
                foreach (DataRow r in dt.Rows)
                {
                    if (int.Parse(r["pid"].ToString()) == 0)
                    {
                        TreeNode dn = new TreeNode();
                        dn.Text = r["text"].ToString();
                        dn.Value = r["value"].ToString();
                        GetNodes(dn, Convert.ToInt32(r["id"]), dt);
                        dn.SelectAction = TreeNodeSelectAction.Expand;//
                        dn.Expanded = false;
                        tree.Nodes.Add(dn);
                    }
                }
            }
        }
        public void GetNodes(TreeNode dn, Int32 i, DataTable dt)
        {
            foreach (DataRow re in dt.Select("pid = " + i))
            {
                TreeNode d = new TreeNode();
                d.Text = re["text"].ToString();
                d.Value = re["value"].ToString();
                GetNodes(d, Convert.ToInt32(re["id"]), dt);
                d.SelectAction = TreeNodeSelectAction.Expand;//
                d.Expanded = false;
                dn.ChildNodes.Add(d);
            }
        }


        protected void tree_SelectedNodeChanged(object sender, EventArgs e)
        {
            tree.SelectedNode.Checked = !tree.SelectedNode.Checked;
            tree.SelectedNode.Selected = false;
        }
        protected void tree_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            SetNodes(e.Node);
        }


        public void SetNodes(TreeNode d)
        {
            foreach (TreeNode f in d.ChildNodes)
            {
                f.Checked = d.Checked;
                if (f.ChildNodes.Count > 0)
                {
                    SetNodes(f);
                }

            }
        }
        protected void _btExport_Click(object sender, EventArgs e)
        {
            List<Hashtable> list = new List<Hashtable>();
            foreach (TreeNode dn in tree.Nodes)
            {
                Hashtable hs = new Hashtable();
                GetCheckNodes(dn, hs);
                list.Add(hs);
            }
            EnvironmentManagerSystem.BLL.TreeViewPanel.Export(list, "00");
        }
        public void GetCheckNodes(TreeNode dn, Hashtable hs)
        {
            foreach (TreeNode d in dn.ChildNodes)
            {
                if (d.ChildNodes.Count > 0)
                {
                    GetCheckNodes(d, hs);
                }
                else if (d.Checked)
                {
                    if(!hs.Contains(d.Text))
                    hs.Add(d.Text, d.Value);
                }
            }
        }

    }
}
